package persentation.presentTeacher;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.*;

import dataservice.ResultMessage;
import businesslogic.userBl.TeacherController;
import businesslogic.userBl.UserController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class ChangePasswordPanel extends JPanel{
	private JButton okButton,noButton;
	private JPasswordField oldPasswordField,newPasswordField,ensurePasswordField;
	private JLabel oldPasswordLabel,newPasswordLabel,ensurePasswordLabel;
	private UserController userController;
	private MyTabbedPane tabbedPane;
	private static Image INFO=new ImageIcon("Graphics/teacherGraphics/info.png").getImage();
	
	public ChangePasswordPanel(String userID) {
		setLayout(null);
		setSize(800,600);
		final String id=userID;
		
		userController=new UserController(userID);
		
		ensurePasswordLabel=new JLabel("请再次输入的新密码");
		ensurePasswordLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
		ensurePasswordLabel.setBounds(new Rectangle(220, 255, 180, 28));
		this.add(ensurePasswordLabel);
		
		
		newPasswordLabel=new JLabel("请输入新密码");
		newPasswordLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
		newPasswordLabel.setBounds(new Rectangle(247, 161, 120, 28));
		this.add(newPasswordLabel);
		
		oldPasswordLabel=new JLabel("请输入旧密码");
		oldPasswordLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
		oldPasswordLabel.setBounds(new Rectangle(247, 73, 120, 28));
		this.add(oldPasswordLabel);
		okButton=new JButton("确认");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				ResultMessage message=
				userController.changePassword(oldPasswordField.getPassword(), 
						newPasswordField.getPassword(), ensurePasswordField.getPassword());
				
				
				if(message==ResultMessage.NO_ACCESS){
					JOptionPane.showMessageDialog(null,"原密码错误，您没有修改密码的权限！","提示",JOptionPane.INFORMATION_MESSAGE);

				}else if(message==ResultMessage.MODIFY_SUCCESS){
					JOptionPane.showMessageDialog(null,"用户密码修改成功！","提示",JOptionPane.INFORMATION_MESSAGE);

					oldPasswordField.setText("");
					newPasswordField.setText("");
					ensurePasswordField.setText("");
					updateUI();
				
					
				}else if(message==ResultMessage.MODIFY_FAIL){
					JOptionPane.showMessageDialog(null,"用户密码修改失败！","提示",JOptionPane.INFORMATION_MESSAGE);

				}
				
			}
		});
		okButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
		okButton.setBounds(291, 364, 73, 37);
		this.add(okButton);
		
		
		oldPasswordField=new JPasswordField();
		oldPasswordField.setFont(new Font("微软雅黑",Font.PLAIN,20));
		oldPasswordField.setBounds(new Rectangle(469, 70, 152, 34));
		this.add(oldPasswordField);
		
		newPasswordField=new JPasswordField();
		newPasswordField.setFont(new Font("微软雅黑",Font.PLAIN,20));
		newPasswordField.setBounds(new Rectangle(469, 158, 152, 34));
		this.add(newPasswordField);
		
		
		noButton=new JButton("取消");
		noButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int message=JOptionPane.showConfirmDialog(null,"确认不再修改密码？","确认",JOptionPane.YES_NO_OPTION);
				if(message==JOptionPane.YES_OPTION){
					tabbedPane.removeOne(tabbedPane.indexOfTab("修改密码"));
					tabbedPane.remove(tabbedPane.indexOfTab("修改密码"));
					
				}else {
					
				}
				
			}
		});
		noButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
		noButton.setBounds(508, 364, 73, 37);
		this.add(noButton);
		
		ensurePasswordField=new JPasswordField();
		ensurePasswordField.setFont(new Font("微软雅黑",Font.PLAIN,20));
		ensurePasswordField.setBounds(new Rectangle(469, 252, 152, 34));
		this.add(ensurePasswordField);
		
		
		
	}
	
	
	public void setTabbedPane(MyTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}
	public void paintComponent(Graphics g){
		g.drawImage(INFO, 0,0, null);
	}

}
